﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;

public partial class controls_admin_Categories : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
            loadCategories();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
    }
    private void loadCategories()
    {
        try
        {
            base.OpenConnection();
            int c_id;
            if (Request["c_id"] != null)
                c_id = Convert.ToInt32(Request["c_id"]);
            else
                c_id = 0;
            DataTable objDT = BO_Categories.SelectParent(c_id, 1);
            base.bindRepeater(rptData, objDT, divMessage);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            base.OpenConnection();
            Button btnID = (Button)e.Item.FindControl("btnID");
            int c_id = Convert.ToInt32(btnID.CommandArgument);
            string commandName = e.CommandName;
            if (commandName.Equals("del"))
            {
                BO_Categories.Delete(c_id);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
            }
            loadCategories();
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            base.OpenConnection();
            Button btnID = (Button)e.Item.FindControl("btnID");
            int c_id = Convert.ToInt32(btnID.CommandArgument);
            int count = BO_Categories.SelectParent(c_id, 1).Rows.Count;
            HtmlGenericControl divCountChild = (HtmlGenericControl)e.Item.FindControl("divCountChild");
            if (count > 0)
                divCountChild.InnerHtml = "<a  href='default.aspx?module=categories&c_id=" + c_id + "'>" + count + "</a>";
            else
            {
                divCountChild.InnerHtml = "0";
                HtmlAnchor aGoToChild = (HtmlAnchor)e.Item.FindControl("aGoToChild");
                aGoToChild.HRef = "";
                aGoToChild.Style.Add("Color", "Black");
            }
        }
        catch { }
        finally { base.CloseConnection(); }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx?module=categoryadd");
    }
}
